import Poly from "./Poly.js";

export default class ShapeGeo {
    constructor(arr = []) {
        this.rawArr = arr;
        this.triangleArr = [];
        this.nodeArr = [];
        this.triangleObj = [];

        this.createNode();

    }
    //三个点
    //从p0->p1,如果p0->p2在左侧则返回正数,在右侧则返回负数
    cross([p0, p1, p2]) {
        const [ax, ay, bx, by] = [
            p1.x - p0.x,
            p1.y - p0.y,
            p2.x - p0.x,
            p2.y - p0.y,
        ];
        return ax * by - bx * ay;
    }
    //在三角形中返回True
    inTriangle(p, nodeArr) {
        let tempArr = [
            this.cross([nodeArr[0], nodeArr[1], p]),
            this.cross([nodeArr[1], nodeArr[2], p]),
            this.cross([nodeArr[2], nodeArr[0], p]),
        ];
        if (tempArr[0] >= 0 &&
            tempArr[1] >= 0 &&
            tempArr[2] >= 0)
            return true;
        return false;
    }
    //1.顶点逆时针绘制,且索引位置为连续的三个
    //2.p0->p2必须在p0->p1的左侧
    //3.绘制的三角形中不能有别的点
    getTriangle() {
        const { nodeArr, triangleArr } = this;
        const tempArr = nodeArr.concat();
        let index = 0;
        if (nodeArr.length < 3)
            return;
        while (true) {
            if (tempArr.length <= 3) {
                triangleArr.push([tempArr[0], tempArr[1], tempArr[2]]);
                break;
            }
            var nodes = this.getThreeNode(index, tempArr);//第一条
            if (this.cross(nodes) > 0) {//第二条判断
                let inTr = 0;
                for (let i = 0; i < tempArr.length; i++) {//第三条判断
                    if (this.inTriangle(tempArr[i], nodes)) {
                        inTr++;
                    }
                }
                if (inTr == 3) {
                    triangleArr.push(nodes);
                    tempArr.splice(nodes[1].index, 1);
                }
            }
            index++;
            if (index >= tempArr.length) {
                index = 0;
            }
        }
        console.log(triangleArr)
    }
    getThreeNode(index, arr = []) {
        const tempArr = [];
        for (let i = 0; i < 3; i++) {
            if (index >= arr.length) {
                index = 0;
            }
            tempArr.push({ x: arr[index].x, y: arr[index].y, index: index, num: arr[index].num });
            index++;
        }
        return tempArr;
    }
    createNode() {
        const { rawArr, nodeArr } = this;
        let index = 0;
        for (let i = 0; i < rawArr.length; i += 2) {
            nodeArr.push({ x: rawArr[i], y: rawArr[i + 1], num: index });
            index++;
        }
    }
    drawTriangle(gl) {
        const { triangleArr, triangleObj } = this;
        triangleArr.forEach(u => {
            let triangle = new Poly({
                gl: gl,
                vertices: this.triangleToArr(u)
            })
            triangle.draw(["TRIANGLES"]);
            triangleObj.push(triangle);
        })
    }
    drawLine(gl) {
        const { triangleArr, triangleObj } = this;
        triangleArr.forEach(u => {
            let triangle = new Poly({
                gl: gl,
                vertices: this.triangleToArr(u)
            })
            triangle.draw(["LINE_LOOP"]);
            triangleObj.push(triangle);
        })
    }
    triangleToArr(triangle = []) {
        return [
            triangle[0].x, triangle[0].y,
            triangle[1].x, triangle[1].y,
            triangle[2].x, triangle[2].y,
        ];
    }

}